<?php

class FWalletGoldLog extends BaseModel
{

    public function getWithdrawlist($params)
    {
        $page = intval($params['page']);
        $size = isset($params['limit'])?intval($params['limit']):10;
        if ($page > 0) {
            $page = ($page - 1) * $size;
        }
        $conditions = ' 1=1 ';
        if (isset($params['keywords']) && !empty($params['keywords'])) {
            $conditions = " WHERE (Member.phone like '%{$params['keywords']}%' OR Member.nickname like '%{$params['keywords']}%')";
        }
        if ((isset($params['start_time']) && !empty($params['start_time'])) || (isset($params['end_time']) && !empty($params['end_time']))) {
            if ($conditions) {
                $conditions .= " AND (Withdraw.created_at >= '{$params['start_time']}' AND Withdraw.created_at <= '{$params['end_time']}')";
            } else {
                $conditions .= " WHERE (Withdraw.created_at >= '{$params['start_time']}' AND Withdraw.created_at <= '{$params['end_time']}')";
            }
        }

        if (isset($params['user_ids']) && !empty($params['user_ids'])) {
            if ($conditions) {
                $conditions .= " AND Withdraw.user_id in({$params['user_ids']})";
            } else {
                $conditions .= " WHERE Withdraw.user_id in({$params['user_ids']})";
            }
        }
        $phql            = "SELECT Withdraw.id,Withdraw.user_id,Withdraw.money,Withdraw.order_no,
                 Withdraw.created_at,Withdraw.alipay_account,Member.phone,Member.nickname,Member.avatar,Member.invite_code,Member.alipay_account,Member.real_name
                FROM f_wallet_gold_log as Withdraw 
                LEFT JOIN f_users as Member ON Withdraw.user_id = Member.id where 
                {$conditions}
                ORDER BY Withdraw.id DESC limit $page,$size";
        $result['items']            = $this->getDI()->get('db')->fetchAll($phql);
        $phql = "SELECT count(*) as count FROM f_wallet_gold_log as Withdraw 
                LEFT JOIN f_users as Member ON Withdraw.user_id = Member.id where 
                {$conditions}";
        $total = $this->getDI()->get('db')->fetchOne($phql);
        $result['total'] = $total['count'];
        return $result;
    }

    public function money_totallist(){
        $money = $this->findFirst(array(
            'columns'=>'sum(money) as money'
        ));
        $mo = $money->money ? $money->money : 0;
        return $mo;
    }

    public function get_list($params){
        $page = isset($params['page']) ? $params['page'] : 1;
        $size = 10;
        if ($page > 0) {
            $page = ($page - 1) * $size;
        }
        $condition = '1=1 ';

        if (isset($params['phone']) && !empty($params['phone'])) {
            $userDb = new User();
            $info   = $userDb->findFirstByPhone($params['phone']);
            if (!$info) {
                return [
                    'count' => 0,
                    'total' => 0,
                    'list' => [],
                ];
            }
            $condition .= " and user_id = $info->id";
        }
        if (isset($params['start_time']) && !empty($params['start_time'])) {
            $condition .= " and created_at >= '{$params['start_time']}'";
        }

        if (isset($params['user_id']) && !empty($params['user_id'])) {
            $condition .= " and user_id = {$params['user_id']}";
        }

        if (isset($params['end_time']) && !empty($params['end_time'])) {
            $condition .= " and created_at <= '{$params['end_time']}'";
        }
        $result['count'] = $this->countGold($condition);
        $result['total'] = $this::count($condition);
        $result['list']  = $this::find(
            array(
                $condition,
                'columns' => 'user_id,money as num,created_at',
                "order"  => 'id desc',
                'limit'  => $size,
                'offset' => $page
            ))->toarray();
        if(!empty($result['list'])){
            foreach ($result['list'] as $key=>$val){
                $result['list'][$key]['remarks'] = '金币提现';
            }
        }
        return $result;
    }
    public function countGold($condition){
        $where['conditions'] = $condition;
        $where['columns'] = "sum(money) as num";
        $num = $this->findFirst($where);
        return $num->num?$num->num:0;
    }

}
